package swagenda.ui;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.TreeSet;

import javax.swing.JPanel;

import org.apache.log4j.Logger;

import swagenda.data.Category;
import swagenda.data.Service;
import swagenda.proxy.OosProxy;

@SuppressWarnings("serial")
public class AlphaPanel extends JPanel
{
	Logger logger = Logger.getLogger(this.getClass());
	public final static String[] ALPHA_ARRAY = { "a", "b", "c", "d", "e", "f", "g",
		"h", "i", "j", "k", "l", "m", "n" , "o" , "p" , "q" , "r" , "s" , "t" 
		, "u" , "v" , "w" , "x" , "y" , "z" };
	public final static int[] MNEMONICS = {KeyEvent.VK_F10, KeyEvent.VK_F11, KeyEvent.VK_A, KeyEvent.VK_B, KeyEvent.VK_C, KeyEvent.VK_D, KeyEvent.VK_E, KeyEvent.VK_F, KeyEvent.VK_G,
		KeyEvent.VK_H, KeyEvent.VK_I, KeyEvent.VK_J, KeyEvent.VK_K, KeyEvent.VK_L, KeyEvent.VK_M, KeyEvent.VK_N , KeyEvent.VK_O , KeyEvent.VK_P , KeyEvent.VK_Q , KeyEvent.VK_R, KeyEvent.VK_S, KeyEvent.VK_T, 
		KeyEvent.VK_U, KeyEvent.VK_V, KeyEvent.VK_W, KeyEvent.VK_X, KeyEvent.VK_Y, KeyEvent.VK_Z 
	};
	public final static String[] TAB_ARRAY = {"all", "#", "a", "b", "c", "d", "e", "f", "g",
		"h", "i", "j", "k", "l", "m", "n" , "o" , "p" , "q" , "r" , "s" , "t" 
		, "u" , "v" , "w" , "x" , "y" , "z" 
	};
	public final String ALL = "all";
	public final static String STRANGE = "#";
	public OosProxy oosProxy;
	public int alpha_index;
	public TreeSet<Service> places;
	private final int PLACES_PER_LINE = 5;
	
	
	public AlphaPanel(OosProxy oosProxy, int alpha_index) {
		super();
		init(oosProxy, alpha_index, null, null, null);
	}

	public void init(OosProxy oosProxy, int alpha_index, ArrayList<Category> sections, ArrayList<Category> tags, ArrayList<Category> locations)
	{
		this.alpha_index = alpha_index;
		init(oosProxy, sections, tags, locations);
	}
	
	public void init(OosProxy oosProxy, ArrayList<Category> sections, ArrayList<Category> tags, ArrayList<Category> locations)
	{
//		logger.debug("initializing alpha panel "+TAB_ARRAY[alpha_index]);
		removeAll();

		setLayout(new FlowLayout(FlowLayout.LEADING));
		this.oosProxy = oosProxy;
		
		if (TAB_ARRAY[alpha_index].equals(ALL))
			places = oosProxy.owner.places;
		else 
			places = oosProxy.getPlacesStartingWith(TAB_ARRAY[alpha_index]);

		places = filterOut(places, sections);
		places = filterOut(places, tags);
		places = filterOut(places, locations);

		for (Service p: places)
		{
			PlacePanelSmall pps = new PlacePanelSmall(p);
			add(pps);
		}
		int panelYSize = PlacePanelSmall.Y_SIZE*(places.size()+PLACES_PER_LINE*2)/PLACES_PER_LINE;
		int panelXSize = PlacePanelSmall.X_SIZE*PLACES_PER_LINE;
		if (TAB_ARRAY[alpha_index].equals(ALL))
		{
			logger.debug("places.size(): "+places.size());
			logger.debug("small panel xSize: "+PlacePanelSmall.X_SIZE);
			logger.debug("whole line xSize: "+panelXSize);
		}
		setPreferredSize(new Dimension(panelXSize+30,panelYSize));
	}
	
	private TreeSet<Service> filterOut(TreeSet<Service> places, ArrayList<Category> selected)
	{
//		logger.debug("filters: "+selected);
		if (selected==null || selected.size()==0) return places;
		
		TreeSet<Service> filtered = new TreeSet<Service>();
		for (Service p: places)
		{
			TreeSet<Category> pCats = new TreeSet<Category>();
			if (selected.get(0).type.equals(Category.Type.SECTION)) pCats = p.sections;
			if (selected.get(0).type.equals(Category.Type.TAG)) pCats = p.tags;
			if (selected.get(0).type.equals(Category.Type.LOCATION)) pCats.add(p.locality);
			for (Category c: pCats)
			{
				for (Category s: selected)
				{
					if (s.equals(c))
					{
						filtered.add(p);
						break;
					}
				}
			}
		}
		return filtered;
	}
}
